package com.zcl.utils;

import com.zcl.bean.UserInfo;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

public class Preferences {
	private SharedPreferences mPref;
	private Editor mEditor;
	private static Preferences instance = null;

	public Preferences(Context context) {
		mPref = context.getSharedPreferences("imconfig", 0);
		mEditor = mPref.edit();
	}

	public static synchronized Preferences getInstance(Context context) {
		if (instance == null) {
			instance = new Preferences(context);
		}
		return instance;
	}
	
	/**
	 * 将登录用户写入配置文件.
	 * @param info
	 */
	public void setLoginUser(UserInfo info)
	{
		mEditor.putString(UserInfo.USER_ID, info.getUserId());
		mEditor.putString(UserInfo.USER_NAME, info.getUserName());
		mEditor.putString(UserInfo.USER_TOKEN, info.getToken());
		mEditor.putString(UserInfo.USER_TOKENSECRET, info.getTokenSecret());
		mEditor.putString(UserInfo.IS_DEFAULT, info.getIsDefualt());
		mEditor.commit();
	}
	
	/**
	 * 将登录用户从配置文件中取出
	 * @return
	 */
	public UserInfo getLoginUser()
	{
		UserInfo info = null;
		if (TextUtils.isEmpty(mPref.getString(UserInfo.USER_ID, "")) || TextUtils.isEmpty(mPref.getString(UserInfo.USER_NAME, ""))) {
			return info;
		}
		String user_id = mPref.getString(UserInfo.USER_ID, "");
		String user_name = mPref.getString(UserInfo.USER_NAME, "");
		String user_token = mPref.getString(UserInfo.USER_TOKEN, "");
		String user_tokenSecret = mPref.getString(UserInfo.USER_TOKENSECRET, "");
		String user_isdefault = mPref.getString(UserInfo.IS_DEFAULT, "");
		return new UserInfo(user_id, user_name, user_token, user_tokenSecret, user_isdefault);
	}
}
